Conditions | 5 |
Paths | 400 |
Total Lines | 133 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var Gfr = function($el, opts) { |
||
2 | var _this = this; |
||
3 | |||
4 | _this.options = $.extend( |
||
5 | { |
||
6 | autoPlay: false, |
||
7 | positioning: "50% 50%" /* String, CSS Background positioning */, |
||
8 | preserveAspectRatio: true, |
||
9 | sizeToFit: true, |
||
10 | $target: jQuery($el.data("target")), |
||
11 | axis: $el.data("axis") || false, |
||
12 | frameDimensions: $el.data("frame").split("x"), |
||
13 | sourceDirection: $el.data("sourceDirection") || "horziontal" |
||
14 | }, |
||
15 | opts |
||
16 | ); |
||
17 | |||
18 | console.log(_this.options); |
||
|
|||
19 | |||
20 | // Direction |
||
21 | _this.directionOfClip = 1; |
||
22 | _this.$el = $el; |
||
23 | _this.$target = _this.options.$target.length |
||
24 | ? _this.options.$target |
||
25 | : _this.$el; |
||
26 | _this.imgSrc = _this.$el.data("src"); |
||
27 | _this.$filmStrip = $( |
||
28 | '<img src="' + |
||
29 | _this.$el.data("src") + |
||
30 | '" class="gifr-filmstrip s__awaiting"/>' |
||
31 | ); |
||
32 | |||
33 | if (_this.options.frameDimensions) { |
||
34 | var fdims = _this.options.frameDimensions; |
||
35 | (_this.originalFrameWidth = (_this.frameWidth = fdims[ |
||
36 | 0 |
||
37 | ])), (_this.originalFrameHeight = (_this.frameHeight = fdims[1])); |
||
38 | } else { |
||
39 | (_this.originalFrameWidth = (_this.frameWidth = 1920)), (_this.originalFrameHeight = (_this.frameHeight = 698)); |
||
40 | } |
||
41 | |||
42 | // Specifics |
||
43 | _this.imgRatio = _this.frameWidth / _this.frameHeight; |
||
44 | |||
45 | _this.$el.append(_this.$filmStrip); |
||
46 | |||
47 | // Bailout no image found! |
||
48 | if (!_this.imgSrc) { |
||
49 | return; |
||
50 | } |
||
51 | |||
52 | _this.containerHeight = _this.$target.outerHeight(); |
||
53 | _this.containerWidth = _this.$target.outerWidth(); |
||
54 | |||
55 | _this.counter = 0; |
||
56 | _this.imgPointer = 0; |
||
57 | |||
58 | if (_this.options.preserveAspectRatio) { |
||
59 | _this.containerHeight = _this.containerWidth / _this.imgRatio; |
||
60 | } |
||
61 | |||
62 | _this.setup(); |
||
63 | |||
64 | // Element to bind mousemove to? |
||
65 | var $mouseEl = _this.$target || jQuery(document); |
||
66 | |||
67 | _this.pMouseX = 0; |
||
68 | _this.pMouseY = 0; |
||
69 | _this.controlAxis = _this.options.axis || "auto"; |
||
70 | |||
71 | // console.log($mouseEl); |
||
72 | |||
73 | $mouseEl.on("mousemove", function(evt) { |
||
74 | clearInterval(_this.timer); |
||
75 | let control = _this.controlAxis; |
||
76 | |||
77 | if (control === "auto" && (_this.pMouseX > 0 && _this.pMouseY > 0)) { |
||
78 | var diffX = Math.abs(evt.pageX - _this.pMouseX); |
||
79 | var diffY = Math.abs(evt.pageY - _this.pMouseY); |
||
80 | |||
81 | // console.log({x:diffX, y:diffY}); |
||
82 | if (diffX === 0 && diffY === 0) { |
||
83 | return; |
||
84 | } |
||
85 | |||
86 | if (diffX > diffY) { |
||
87 | _this.controlAxis = (control = "x"); |
||
88 | } else { |
||
89 | _this.controlAxis = (control = "y"); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | _this.pMouseX = evt.pageX; |
||
94 | _this.pMouseY = evt.pageY; |
||
95 | |||
96 | var pointer = false; |
||
97 | |||
98 | if (control === "x") { |
||
99 | let x = evt.clientX - $mouseEl.offset().left; |
||
100 | pointer = Math.round( |
||
101 | _this.map(x, 0, _this.$target.outerWidth(), 0, _this.frameCount() - 1) |
||
102 | ); |
||
103 | } else if (control === "y") { |
||
104 | pointer = Math.round( |
||
105 | _this.map( |
||
106 | evt.clientY, |
||
107 | 0, |
||
108 | _this.$target.outerHeight(), |
||
109 | 0, |
||
110 | _this.frameCount() - 1 |
||
111 | ) |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | if (pointer < 0) { |
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | _this.positionFrame(pointer); |
||
120 | _this.$filmStrip.removeClass("s__awaiting"); |
||
121 | |||
122 | clearTimeout(_this.activity); |
||
123 | |||
124 | if (_this.options.autoPlay) { |
||
125 | _this.activity = setTimeout( |
||
126 | function() { |
||
127 | _this.start(); |
||
128 | }, |
||
129 | 1000 / 30 |
||
130 | ); |
||
131 | } |
||
132 | }); |
||
133 | }; |
||
134 | |||
351 |